{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.output_parsers import CommaSeparatedListOutputParser\n",
    "\n",
    "# 创建 CommaSeparatedListOutputParser 实例\n",
    "output_parser = CommaSeparatedListOutputParser()\n",
    "\n",
    "# 定义模型输出的示例文本\n",
    "output_text = \"apple, banana, orange, mango\"\n",
    "\n",
    "# 使用 CommaSeparatedListOutputParser 解析输出文本\n",
    "parsed_output = output_parser.parse(output_text)\n",
    "\n",
    "# 打印解析后的输出\n",
    "print(parsed_output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.output_parsers import PydanticOutputParser\n",
    "from pydantic import BaseModel, Field\n",
    "from typing import Dict\n",
    "\n",
    "# 定义规格的Pydantic模型\n",
    "class Specifications(BaseModel):\n",
    "    brand: str = Field(description=\"品牌\")\n",
    "    model: str = Field(description=\"型号\")\n",
    "    features: Dict[str, str] = Field(description=\"产品特性\")\n",
    "\n",
    "# 定义产品的Pydantic模型\n",
    "class Product(BaseModel):\n",
    "    name: str = Field(description=\"产品名称\")\n",
    "    category: str = Field(description=\"产品类别\")\n",
    "    price: float = Field(description=\"产品价格（美元）\")\n",
    "    specifications: Specifications = Field(description=\"产品规格\")\n",
    "\n",
    "# 创建 PydanticOutputParser 实例\n",
    "output_parser = PydanticOutputParser(pydantic_object=Product)\n",
    "\n",
    "# 定义模型输出的示例文本\n",
    "output_text = '''\n",
    "{\n",
    "    \"name\": \"Galaxy S21\",\n",
    "    \"category\": \"手机\",\n",
    "    \"price\": 799.99,\n",
    "    \"specifications\": {\n",
    "        \"brand\": \"Samsung\",\n",
    "        \"model\": \"S21\",\n",
    "        \"features\": {\n",
    "            \"屏幕\": \"6.2英寸 AMOLED\",\n",
    "            \"处理器\": \"Exynos 2100\",\n",
    "            \"存储\": \"128GB\",\n",
    "            \"相机\": \"64MP 主摄 + 12MP 超广角 + 12MP 长焦\"\n",
    "        }\n",
    "    }\n",
    "}\n",
    "'''\n",
    "\n",
    "# 使用 PydanticOutputParser 解析输出文本\n",
    "parsed_output = output_parser.parse(output_text)\n",
    "\n",
    "# 打印解析后的输出\n",
    "print(parsed_output)\n",
    "# 访问产品名称\n",
    "print(parsed_output.name)  # 输出: Galaxy S21\n",
    "\n",
    "# 访问产品类别\n",
    "print(parsed_output.category)  # 输出: 手机\n",
    "\n",
    "# 访问产品价格\n",
    "print(parsed_output.price)  # 输出: 799.99\n",
    "\n",
    "# 访问产品品牌\n",
    "print(parsed_output.specifications.brand)  # 输出: Samsung\n",
    "\n",
    "# 访问产品的屏幕规格\n",
    "print(parsed_output.specifications.features['屏幕'])  # 输出: 6.2英寸 AMOLED\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatchat",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
